<?php
require "../common/user_online.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<?php require "../components/head.php" ?>
	<script type="text/javascript" src="../scripts/changuserinfo_script.js"></script>
</head>
<body onload="MakeCurrentPage(1);">
<div id="container">
	<?php require "../components/user_menu.php" ?>
	<?php require "../components/user_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right" align="center">
		<?php
		include "../common/db.inc";
		$userid = $_SESSION['user_id'];

		$mysqli = new mysqli($host, $username, $password,$dbname);
		$mysqli->set_charset('utf8');	
		/* check connection */
		if (mysqli_connect_errno()) {
		    printf("Connect failed: %s\n", mysqli_connect_error());
		    exit();
		}
		$sql = "CALL sp_GetUserByID($userid)";
		$result = $mysqli->query($sql);
		if ($mysqli->errno) 
		{
			die("Lỗi:".$mysqli->error);
			$mysqli->close();
			
			exit();
		}//end if
		if($result->num_rows!=1)
		{
			echo "Tài khoản này không tồn tại";
			
			$mysqli->close();
			exit();
		}
		$row = $result->fetch_object();
		$email = $row->Email;
		$userName = $row->Name;
		$mana = $row->Mana;

		echo "<h1>CẬP NHẬT THÔNG TIN TÀI KHOẢN</h1>";
        	echo "<form action='xl_ChangeUserInfo.php' method='post' name='frmChangeUserInfo'>";
                echo "<img id='loadWait' src='../images/loader.gif' style='display:none'/>";
                echo "<table class='normal'>";
                     echo "<tr>";
                        echo "<td>Tên đăng nhập </td>";
                        echo "<td><input type='text' name='txtUsername' size='30' length = '30' disabled=true value='$userName'/></td>";
                    echo "</tr>";	
					 echo "<tr>";
                        echo "<td>Mana </td>";
                        echo "<td><input type='text' name='txtMana' size='30' length = '30' disabled=true value='$mana'/></td>";
                    echo "</tr>";	
					echo "<tr>";
                        echo "<td>Mật khẩu cũ <font color='red'>(*)</font></td>";
                        echo "<td><input type='password' name='txtOldPassword' size='30' length='20' onblur='KTOldPass(this.value)'/> </td>";
                    echo "</tr>";
					echo "<tr>";
                        echo "<td></td>";
                        echo "<td><label id='lbOldPassword'></label></td>";
                    echo "</tr>";				
					echo "<tr>";
                        echo "<td>Mật khẩu mới </td>";
                        echo "<td><input type='password' name='txtPassword1' size='30' length='20' onblur='KTPassUpdate(this.value)'/> </td>";
                    echo "</tr>";
					echo "<tr>";
                        echo "<td></td>";
                        echo "<td><label id='lbPassword1'></label></td>";
                    echo "</tr>";
                   echo "<tr>";
                        echo "<td>Nhập lại mật khẩu mới</td>";
                        echo "<td><input type='password' name='txtPassword2' size='30'  onblur='KTPass1_Pass2()'/></td>";
                    echo "</tr>";
                    echo "<tr>";   
                        echo "<td></td>";
                        echo "<td> <label id='lbPassword2'></label></td>";
                    echo "</tr>";
                    echo "<tr>";
                        echo "<td>Email </td>";
                        echo "<td><input type='text' name='txtEmail' size='50' maxlength='30' value='$email'  onblur='KTEmailUpdate(this.value)'/></td>";
                    echo "</tr>";
                    echo "<tr>";
                        echo "<td></td>";
                        echo "<td><label id='lbEmail'></label></td>";
                    echo "</tr>";
                    echo "<tr>";
                        echo "<td>Lĩnh vực yêu thích</td>";
                        echo "<td id='tdLinhVuc'>";
                            $list = getFieldUserInterest($userid);
                        echo "</td>";
                    echo "</tr>";
                    echo "<tr>";
                        echo "<td ></td>";
                        echo "<td><input type='button' value='Cập nhật' id='bntCapNhat' onclick='KTCapNhat()'/></td>";
                    echo "</tr>";
                echo "</table>";
            echo "</form>";
		?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>

<?php	
function getFieldUserInterest($id)
{
	include "../common/db.inc";
	$mysqli = new mysqli($host,$username,$password,$dbname);

	if(mysqli_connect_errno())
	{
		echo "Connect failed: ".mysqli_connect_errno();
		return;
	}
	else
	{
		$mysqli->set_charset("utf8");
		$sql="CALL sp_GetAllField()";
		$result = $mysqli->query($sql);

		if ($mysqli->errno) 
		{
			die($mysqli->errno.": ".$mysqli->error);
			$mysqli->close();
			exit();
		}
		if($result->num_rows == 0)
		{
			echo "Chưa có lĩnh vực nào";
			$mysqli->close();
			exit();
		}
		//get field that user interest
		$mysqliIF = new mysqli($host,$username,$password,$dbname);
		$mysqliIF->set_charset("utf8");
		if(mysqli_connect_errno())
		{
			die('Kết nối thất bại!');
			exit();
		}
		
		//call stored procedure
		$sqlIF = "CALL sp_GetInterestByUserId($id)";
		$resultIF = $mysqliIF->query($sqlIF);
		
		//error
		if($mysqliIF->errno)
		{
			echo "Kết nối thất bại!";
			$mysqliIF->close();
			$mysqli->close();
			exit();
		}
		$mysqliIF->close();
		$list = convertToArray($resultIF);
		
		$i = 0;
		echo "<table>";
		while($row = $result->fetch_object())
		{
			if($i == 0)
			{
				echo "<tr>";
			}//end if
					
			echo "<td>";
			echo "<input type='checkbox' name='checkbox[]' value='$row->ID'";
			if(isExistsField($row->ID,$list))
			{
				echo " checked = TRUE";
			}
			echo " />$row->Name";
			
			echo "</td>";
			$i++;
			
			if($i > 2)
			{
				echo "</tr>";
				$i = 0;
			}//end if	
					
		}// end while
		
		echo "</table>";
	
	}//end if
	$mysqli->close();
	return $list;
}//end function getField

function convertToArray($results)
{
	$i = 0;
	if($results->num_rows > 0)
	{
		while($row = $results->fetch_object())
		{
			$list[$i] = $row->Field;
			$i++;
		}//end while
	}//end if
	return $list;
}//end function convertToArray

function isExistsField($fieldid,$list)
{
	for($i = 0; $i < count($list);$i++)
	{
		if($fieldid == $list[$i])
			return TRUE;
	}//end foreach
	return FALSE;
}//end function isExistsField
?>